package sjs.sys;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletResponse;

import sjs.util.Common;
 
public class WebFilter implements Filter{
 private String       encodingName;
 private final String paramName = "encodingName";
 protected FilterConfig filterConfig = null;

 /*
  * (non-Javadoc)
  * 
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 public void init(FilterConfig arg0) throws ServletException {
  this.filterConfig=arg0; 
  encodingName = arg0.getInitParameter(paramName);
 }

 /*
  * (non-Javadoc)
  * 
  * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
  */
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  try {
	  System.out.println("Filter at This and Time:"+Common.getTime18bit());
   HttpServletRequest httprequest = (HttpServletRequest) request;
   HttpServletResponse httpresponse = (HttpServletResponse) response;
   if (false) {//httprequest.getSession().getAttribute(com.pos.util.Common.SESSION_USER) == null
    String currentUrl = httprequest.getRequestURI();
    //System.out.println("currentUrl:"+currentUrl);
    if (currentUrl == null || currentUrl.indexOf("login.jsp") < 0) {
    	if(currentUrl.indexOf("loginpassword.jsp")<0){
    		httpresponse.sendRedirect(httprequest.getContextPath() + "/home/login.jsp");
    		 return;
    	}
    }
   }
//    if (request.getCharacterEncoding() == null) {
//        String encoding = selectEncoding(request);
//        if (encoding != null)
//            request.setCharacterEncoding(encoding);
//    }
    chain.doFilter(request, response);
  } finally {
	 // HibernateSessionFactory.closeSession();
  }
 }
 protected String selectEncoding(ServletRequest request) {
     return (this.encodingName);
 }
 public void destroy() {
     this.encodingName = null;
     this.filterConfig = null;
 }

}
